package cn.sc.summer.mybatis.model;

import lombok.Data;

import javax.sql.DataSource;
import java.io.Closeable;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.Properties;
import java.util.logging.Logger;

/**
 * 类名：数据源类型
 *
 * @author a-xin
 * @date 2024/8/29 15:06
 */
@Data
public class DataSourceX implements DataSource, Closeable {

    private static final long serialVersionUID = -537906631299766044L;

    /**
     * 数据源名称
     */
    private String dataSourceName;

    /**
     * 数据源类型名称
     */
    private String dataSourceTypeName;

    /**
     * 驱动配置
     */
    private String driver;

    /**
     * 数据源链接
     */
    private String url;

    /**
     * 数据源用户
     */
    private String user;

    /**
     * 数据源密码
     */
    private String pass;

    /**
     * 初始化
     *
     * @param url    jdbc url
     * @param user   用户名
     * @param pass   密码
     * @param driver JDBC驱动类
     * @since 3.1.2
     */
    public void init(String url, String user, String pass, String driver) {
        this.driver = driver;
        try {
            Class.forName(this.driver);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException("Get jdbc driver [" + driver + "] error!");
        }
        this.url = url;
        this.user = user;
        this.pass = pass;
        setLoginTimeout(10);
    }

    @Override
    public Connection getConnection() throws SQLException {
        final Properties properties = new Properties();
        if (this.user != null) {
            properties.setProperty("user", this.user);
        }
        if (this.pass != null) {
            properties.setProperty("password", this.pass);
        }
        return DriverManager.getConnection(this.url, properties);
    }

    @Override
    public Connection getConnection(String username, String password) throws SQLException {
        return DriverManager.getConnection(this.url, username, password);
    }

    @Override
    public void close() {
        // Not need to close;
    }

    @Override
    public PrintWriter getLogWriter() {
        return DriverManager.getLogWriter();
    }

    @Override
    public void setLogWriter(PrintWriter out) {
        DriverManager.setLogWriter(out);
    }

    @Override
    public void setLoginTimeout(int seconds) {
        DriverManager.setLoginTimeout(seconds);
    }

    @Override
    public int getLoginTimeout() {
        return DriverManager.getLoginTimeout();
    }

    @Override
    public <T> T unwrap(Class<T> clazz) throws SQLException {
        throw new SQLException("Can't support unwrap method!");
    }

    @Override
    public boolean isWrapperFor(Class<?> clazz) throws SQLException {
        throw new SQLException("Can't support isWrapperFor method!");
    }

    /**
     * Support from JDK7
     *
     * @since 1.7
     */
    @Override
    public Logger getParentLogger() throws SQLFeatureNotSupportedException {
        throw new SQLFeatureNotSupportedException("DataSource can't support getParentLogger method!");
    }

}
